var ROOT = '';
var SERVLET_ROOT = '';

ACTION_LOGIN = 1;
ACTION_OPENLIST = 2;
ACTION_OPEN = 3;
ACTION_SAVE = 4;
ACTN_CANCEL = 12;
ACTION_PROCESS = 40;
ACTION_REMOVE = 41;
ACTION_DONE = 42;
ACTION_FINISH = 46;
ACTION_SEARCH = 47;
ACTION_UPDATE = 43;
ACTION_BACK = 44;
ACTION_NEW_USER = 45
ACTION_LOGOUT = 5;
ACTION_CONTINUE = 48;
ACTION_NEXT = 50;
ACTION_END = 50;
ACTION_START = 50;
ACTION_PREVIOUS = 50;
ACTION_SET_VALUE = 50;
function getDataFrame ()
{
    return self;
}

function getDataDocoument ()
{
    return getDataFrame ().document;
}

function getDataForm ()
{
    return getDataDocoument ().DataForm;
    /*
        return getDataDocoument().DataForm;
    */
}
function performSearch ()
{
    processRequest (ACTION_SEARCH, "N");
}
function performNext ()
{
    processRequest (ACTION_NEXT, "next");
}
function performEnd ()
{
    processRequest (ACTION_END, "end");
}
function performPrevious ()
{
    processRequest (ACTION_PREVIOUS, "previous");
}
function performStart ()
{
    processRequest (ACTION_START, "start");
}
function performSave ()
{
    processRequest (ACTION_SAVE);
}
function logout ()
{
    if (arguments.length > 0) ACTION_LOGOUT += "^" + arguments[0];
    processRequest (ACTION_LOGOUT);
}

function performCancel ()
{
    processRequest (ACTN_CANCEL);
}
function performContinue ()
{
    var ls = document.getElementsByName ("pid").length;
    var lsPid = "";
    for (var i = 0; i < ls; i++)
    {
        if (i == (ls - 1))
        {
            if (document.getElementsByName ("pid")[i].checked)
            {
                lsPid += self.document.getElementsByName ("pid")[i].value;
            }
        }
        else
        {
            if (document.getElementsByName ("pid")[i].checked)
            {
                lsPid += self.document.getElementsByName ("pid")[i].value + ":";
            }
        }
    }
    processRequest (ACTION_CONTINUE, lsPid);
}

function performLogin ()
{
    processRequest (ACTION_LOGIN);
}
function performNewUser ()
{
    processRequest (ACTION_NEW_USER);
}

function performRemove ()
{
    var laRemove = getElementsByNameArray ("RemoveID");
    processRequest (ACTION_REMOVE, "N", laRemove);
}
function performHome ()
{
    logout ("N");
}
function performBack ()
{
    processRequest (ACTION_BACK);
}
function performDone ()
{
    processRequest (ACTION_DONE);
}
function performFinish ()
{
    processRequest (ACTION_FINISH);
}

function setSelected (totalrow, fsTag)
{
    if (fsTag == "remove")
    {
        var ls = document.getElementsByName ("Pid").length;
        for (i = 0; i < ls; i++)
        {
            document.getElementsByName ("pid")[i].checked = true;
        }
        setEnabled ('remove', true);
        setEnabled ('update', true);
    }
}
function setEnabled (fsName, fbEnabled)
{
    loButton = document.getElementById (fsName);
    if (loButton)
    {
        loButton.disabled = !fbEnabled;
    }
}
function getElementsByNameArray (fsElementName)
{
    var liLength = document.getElementsByName (fsElementName).length;
    var count = 0;
    var laValue;
    for (var j = 0; j < liLength; j++)
    {
        if (document.getElementsByName (fsElementName)[j].checked)count++;
    }
    ;
    laValue = new Array (count);
    count = 0;
    for (var k = 0; k < liLength; k++)
    {
        if (document.getElementsByName (fsElementName)[k].checked)
        {
            laValue[count] = self.document.getElementsByName (fsElementName)[k].value;
            count++
        }
    }
    return laValue;
}
function performProcess ()
{
    var lspid = getElementsByNameArray ("ProId");
    processRequest (ACTION_PROCESS, "N", lspid);
}
function performUpdate ()
{
    var length = 0;
    var laRemove = getElementsByNameArray ("RemoveID");
    length = document.getElementsByName ("quantity").length;
    var laQty = new Array (length);
    var laProId=new Array(length);
    for (var j = 0; j < length; j++)
    {
        laQty[j] = document.getElementsByName ("quantity")[j].value;
    }
    length = document.getElementsByName ("quantityDis").length;
    var laQtyDis = new Array (length);
    for (var j = 0; j < length; j++)
    {
        laQtyDis[j] = document.getElementsByName ("quantityDis")[j].value;
        laProId[j] = document.getElementsByName ("proid")[j].value;
    }
    processRequest (ACTION_UPDATE, laQty, laQtyDis, laRemove, laProId);
}

function setPageValue (fsPageName)
{
    processRequest (ACTION_SET_VALUE, fsPageName)

}
function getEventCode (event)
{
    if (event.keyCode == 116)
    {
        alert (event.keyCode);
    }
}

function openPage (fsPageName)
{
    if (arguments.length == 1)
    {
        processRequest (ACTION_OPEN, fsPageName)
    }
    else
    {
        processRequest (ACTION_OPEN, fsPageName, arguments[1])
    }
}
function processRequest (fsAction)
{
    lsAction = fsAction;
    if (arguments.length > 1) lsAction += "^" + arguments[1];
    if (arguments.length > 2) lsAction += "^" + arguments[2];
    if (arguments.length > 3) lsAction += "^" + arguments[3];
    if (arguments.length > 4) lsAction += "^" + arguments[4];
    submitRequest (lsAction);
}

function submitRequest (fsAction)
{
    loDocument = getDataDocoument ();
    loDocument.body.style.cursor = "wait";
    loForm = getDataForm ();
    loForm.setAttribute ("action", top.SERVLET_ROOT + "ROController");
    loForm.setAttribute ("method", "post");
    loForm.action_fld.value = fsAction;
    loForm.submit ();
}
function openList ()
{
    processRequest (ACTION_OPENLIST, "N");
}

function startTime ()
{
    var today = new Date ();
    var Days = new Array (8) ;
    var Months = new Array (13);
    var day = today.getUTCDay ();
    var month = today.getMonth () ;
    var year = today.getYear () ;
    var date = today.getDate ();
    var h = today.getHours ();
    var m = today.getMinutes () ;
    var s = today.getSeconds () ;
    // add a zero in front of numbers<10
    m = checkTime (m)
    s = checkTime (s)
    Months[0] = "Jan";
    Months[1] = "Feb";
    Months[2] = "Mar";
    Months[3] = "Apr";
    Months[4] = "May";
    Months[5] = "Jun";
    Months[6] = "Jul";
    Months[7] = "Aug";
    Months[8] = "Sep";
    Months[9] = "Oct";
    Months[10] = "Nov";
    Months[11] = "Dec";
    Days[0] = "Sun";
    Days[1] = "Mon";
    Days[2] = "Tue";
    Days[3] = "Wed";
    Days[4] = "Thu";
    Days[5] = "Fri";
    Days[6] = "Sat";
    var AmOrPm = "AM";
    if (h >= 12)
    {
        if (h > 12)
        {
            h = h - 12;
        }
        AmOrPm = 'PM';
    }
    document.getElementById ('txt').innerHTML = "<b><font color='white' >" +
                                                "<font color='yellowgreen' size='3' style=font-weight:bold;>Date: </font>"
            + date + "/" + Months[month] + "/" + year + "<br>" +
                                                "<font color='yellowgreen' size='3' style=font-weight:bold;>Time: </font>"
            + h + ":" + m + ":" + s + ":" + AmOrPm + "</br></font></b>";

    t = setTimeout ('startTime()', 500);
}
function checkTime (i)
{
    if (i < 10)
    {
        i = "0" + i;
    }
    return i;
}

function displayImage(fsimgName, rowno)
{
    loSection = document.getElementById ("DisplaySection");
    loRow=document.getElementById("row_no_"+rowno);
    if (loSection)
    {
        loSection.style.display = "block";
        loImage = document.getElementById (loSection.id + "Image");
        loImage.src = "img1/" + fsimgName;
        loRow.bgColor="#9999ff";
    }

}
function hideImage(fsimgName, rowno)
{
    loSection = document.getElementById ("DisplaySection");
    loRow=document.getElementById("row_no_"+rowno);
    if (loSection)
    {
        loSection.style.display = "block";
        loImage = document.getElementById (loSection.id + "Image");
        loImage.src = "img/image.gif";
        loRow.bgColor = (rowno % 2 == 0) ? "#f7e5d2" : "#d2decd";
    }

}






